package com.example.goatstore;

import mapping.Usuario;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity 
{

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        final EditText txt1 = (EditText)findViewById(R.id.editTextCorreo);
        final EditText txt2 = (EditText)findViewById(R.id.editTextPass);
        final Button btnHola = (Button)findViewById(R.id.buttonIngresar);
        
        btnHola.setOnClickListener(new OnClickListener() 
        {
                    public void onClick(View v) 
                    {
                        //Creamos la información a pasar entre actividades
                        if(txt1.getText().toString().split(" ").length==0 
                                        || txt2.getText().toString().split(" ").length==0)
                        {
                                                Toast.makeText(getApplicationContext(),
                                                                "Usuario y/o Password Vacíos ", Toast.LENGTH_SHORT).show();
                        }
                        else
                        {
                                if(validate(txt1.getText().toString(),txt2.getText().toString()))
                                {                               
                                Bundle b = new Bundle();
                                Usuario u=control.ControlUsuario.obtenerUsuarioEmail(txt1.getText().toString());
                                Log.d("tipo", u.getTipo());
                                Log.d("pass", u.getPass());
                                Log.d("nombre", u.getNombre());
                                Log.d("apellido", u.getApellido());
                                Log.d("numero", u.getNumeroSucursal()+"");
                                Log.d("email", u.getEmail());
                                if(u.getTipo().compareTo("admin")==0)
                                {
                                        Intent intent = new Intent(LoginActivity.this, PrincipalAdminActivity.class);
                                        b.putLong("iduser", u.getId());
                                        intent.putExtras(b);                                    
                                                startActivity(intent);
                                }                                                               
                                if(u.getTipo().compareTo("empresa")==0)
                                {
                                        Intent intent = new Intent(LoginActivity.this, PrincipalEmpresaActivity.class);
                                        b.putLong("iduser", u.getId());
                                        intent.putExtras(b);                                    
                                                startActivity(intent);
                                }
                                if(u.getTipo().compareTo("basico")==0)
                                {
                                        Intent intent = new Intent(LoginActivity.this, TestActivity.class);
                                        b.putLong("iduser", u.getId());
                                        intent.putExtras(b);                                    
                                                startActivity(intent);
                                }
//                              Intent intent = new Intent(MainActivity.this, TestActivity.class);
//                                      startActivity(intent);
                                }                  
                                else
                                {
                                                        Toast.makeText(getApplicationContext(),
                                                                        "Usuario y/o Password Invalidos", Toast.LENGTH_SHORT).show();
                                }
                        }                
                    }
                });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public boolean validate(String u, String p)
    {

        if(control.ControlUsuario.validar(u, p))
        {
                return true;
        }
        else
        {
                return false;
        }
    }
}